#!/bin/sh
#
# Start filebrowser....
#

NAME=filebrowser
DAEMON="/usr/local/apps/filebrowser/filebrowser"
USER="filebrowser"
GROUP="users"
UMASK="002"

srvname=fm
http_port=$(/usr/bin/nvram get app.fm.fm_http_port)
default_scope=$(/usr/bin/nvram get app.fm.default_scope)

if [ "$http_port" == "" ];then
	http_port=8384
fi

if [ "$default_scope" == "" ];then
	default_scope=/media
fi

FB_EXTRA_ARGS="--cert /etc/dashboard/ssl/ssl.crt --key /etc/dashboard/ssl/ssl.key --port $http_port --address 0.0.0.0 --database /var/lib/filebrowser/filebrowser.db --log /var/log/filebrowser.log --root $default_scope"

func_start() {
	echo "Starting filebrowser..."
	start-stop-daemon -S -b --umask $UMASK --chuid $USER:$GROUP -x $DAEMON -- $FB_EXTRA_ARGS
}

func_stop() {
	echo "Stoping filebrowser..."
	start-stop-daemon -K -q -x $DAEMON
	killall $NAME > /dev/null 2>&1
}

case "$1" in
  start)
		func_start
	;;
  stop)
		func_stop
	;;
  restart|reload)
	echo "Restarting filebrowser..."
		func_stop
		func_start
	;;
  *)
	echo "Usage: $0 {start|stop|restart}"
	exit 1
esac

exit $?
